package com.itheima.service.mongo.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.CommentService;
import com.itheima.util.CommentType;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public int save(Comment comment) {
        //1.往comment表中插入记录
        mongoTemplate.save(comment);

        //2.根据操作的类型修改指定的数量
        switch (comment.getCommentType()) {
            case 1: {//对动态点赞
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改对应的数量 ++
                movement.setLikeCount(movement.getLikeCount() + 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLikeCount();
            }

            case 2: {//对动态评论
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改对应的数量 ++
                movement.setCommentCount(movement.getCommentCount() + 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getCommentCount();
            }

            case 3: {//对动态喜欢
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改对应的数量 ++
                movement.setLoveCount(movement.getLoveCount() + 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLoveCount();
            }

            case 4: {//对动态点赞
                //2.1 查询被操作的对象
                Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
                //2.2 修改对应的数量 ++
                video.setLikeCount(video.getLikeCount() + 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(video);
                //2.4 返回最新的数量
                return video.getLikeCount();
            }

            case 5:
            case 6:
            default:
                return 0;
        }
    }

    @Override
    public int delete(Long userId, ObjectId publishId, Integer commentType) {
        //1.从comment表中删除一条记录
        Query query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(commentType).and("userId").is(userId));
        mongoTemplate.remove(query, Comment.class);

        //2.根据操作的类型修改指定的数量
        switch (commentType) {
            case 1: { //对动态取消点赞
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改对应的数量 --
                movement.setLikeCount(movement.getLikeCount() - 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLikeCount();
            }

            case 2: { //对动态取消评论
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改对应的数量 --
                movement.setCommentCount(movement.getCommentCount() - 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getCommentCount();
            }

            case 3: { //对动态取消喜欢
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改对应的数量 --
                movement.setLoveCount(movement.getLoveCount() - 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLoveCount();
            }

            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }

    // 查询评论列表
    @Override
    public PageBeanVo findByPage(ObjectId publishId, Integer pageNum, Integer pageSize) {
        //1.创建条件对象query(动态id,评论,分页,排序)
        Query query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(2))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //2.分页查询
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //3.查询满足条件的总记录数
        query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(2));
        long count = mongoTemplate.count(query, Comment.class);
        //4.封装pageBeanVo且返回
        return new PageBeanVo(pageNum, pageSize, count, comments);
    }

    // 查询我的动态评论信息
    @Override
    public PageBeanVo findByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {
        //1.创建条件对象(publishiUserId,评论类型,排序,分页)
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //2.查询comment表
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //3.查询总记录数
        query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType));
        long count = mongoTemplate.count(query, Comment.class);

        //4.封装pageBeanVo返回
        return new PageBeanVo(pageNum, pageSize, count, comments);
    }

    //视频点赞
    @Override
    public void saveVideoLike(Comment comment) {
        ObjectId publishId = comment.getPublishId();
        //查看发布者id
        Video byId = mongoTemplate.findById(publishId, Video.class);
        Long userId = comment.getUserId();
        comment.setPublishUserId(userId);
        //新增到Comment
        mongoTemplate.save(comment);
        Update update=new Update();
        update.inc("likeCount",1);
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query = Query.query(criteria);
        mongoTemplate.updateFirst(query,update,Video.class);
    }

    //取消点赞
    @Override
    public void disVideoLike(ObjectId id, Long userId) {
        Criteria criteria = Criteria.where("publishId").is(id).and("userId").is(userId).and("commentType").is(CommentType.LIKE.getType());
        //找出已点赞的记录,并删除
        Comment one = mongoTemplate.findOne(Query.query(criteria), Comment.class);
        mongoTemplate.remove(one);
        //修改当前动态的点赞数
        Query query = Query.query(Criteria.where("id").is(id));
        Update update=new Update();
        update.inc("likeCount",-1);
        mongoTemplate.updateFirst(query,update,Video.class);

    }

    //发布视频评论
    @Override
    public void saveVideoComment(Comment newComment) {
        ObjectId publishId = newComment.getPublishId();
        //查看发布者id
        Video byId = mongoTemplate.findById(publishId, Video.class);
        Long userId = newComment.getUserId();
        newComment.setPublishUserId(userId);
        mongoTemplate.save(newComment);
        Update update=new Update();
        update.inc("commentCount",1);
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query = Query.query(criteria);
        mongoTemplate.updateFirst(query,update,Video.class);

    }

    //评论列表翻页
    @Override
    public PageBeanVo selectPage(Integer page, Integer pagesize, String messageID) {
        //筛选品论
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(messageID)).and("commentType").is(2);
        //查询总数
        long count = mongoTemplate.count(Query.query(criteria), Comment.class);
        Query query = Query.query(criteria).skip((page - 1)).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return new PageBeanVo(page,pagesize,count,commentList);
    }

    /*评论列表*/
    @Override
    public List<Comment> findCommentByMovementId(Integer page, Integer pagesize, String movementId) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(CommentType.COMMENT.getType());
        //分页
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pagesize).limit(pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return commentList;
    }

    @Override
    public Integer findMyLike(Long nowId, ObjectId id) {
        //查看当前动态,我是否点赞
        Criteria criteria = Criteria.where("publishId").is(id).and("userId").is(nowId).and("commentType").is(CommentType.LIKE.getType());
        List<Comment> commentList = mongoTemplate.find(Query.query(criteria), Comment.class);
        if (ObjectUtil.isNotEmpty(commentList)){
            return 1;
        }else {
            return 0;
        }
    }
}
